package com.platform.manager.action.system;

import com.platform.domain.TbSysFunction;
import com.platform.domain.TbSysRole;
import com.platform.domain.result.TreeGridData;
import com.platform.manager.action.BaseAction;
import com.platform.service.FunctionService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-9-18
 * Time: 下午2:34
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/system")
@Service("functionAction")
@ParentPackage("json-default")
public class FunctionAction extends BaseAction {

    @Resource
    private FunctionService functionService;

    private List<TbSysFunction> functions;

    private String roleId;

    /**
     * 查询用户角色权限列表
     *
     * @return
     */
    @Action(value = "queryRoleFunctions", results = {@Result(name = "success", type = "json", params = {"root", "datas"})})
    public String queryRoleFunctions() {
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("roleId",roleId);
            // 查询符合条件的记录总数
            functions = functionService.queryFunctionByRoleId(params);

            if (functions != null) {
                datas = new ArrayList<TreeGridData>();
                for (TbSysFunction fun : functions) {
                    TreeGridData data = new TreeGridData();
                    data.setId(fun.getFunctionId());
                    data.setText(fun.getFunctionName());
                    datas.add(data);
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    @JSON
    public List<TbSysFunction> getFunctions() {
        return functions;
    }

    public void setFunctions(List<TbSysFunction> functions) {
        this.functions = functions;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
}
